package be.ipl.streams.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.ipl.streams.domaine.Game;
import be.ipl.streams.domaine.Player;
import be.ipl.streams.exceptions.ArgumentInvalideException;
import be.ipl.streams.exceptions.GameOverException;
import be.ipl.streams.exceptions.NoPlayersException;
import be.ipl.streams.usecases.GameManagement;
import be.ipl.streams.util.Util;

@WebServlet("/newGame.html")
public class CreateGame extends HttpServlet implements Servlet {
	
	private static final long serialVersionUID = 2340275935290308938L;
	
	@EJB
	private GameManagement gameManagement;
	
	@Override
	protected void doPost (HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Player player = (Player) Controleur.testConnected(this, request, response);
		if (player == null) return;
		String gameName = request.getParameter("gameName");

		try {
			Util.checkString(gameName);
		} catch (ArgumentInvalideException e) {
			request.setAttribute("message",
					"Veuillez entrer un nom pour la partie");
			RequestDispatcher rd = getServletContext().getNamedDispatcher("parties");
			rd.forward(request, response);
			return;
		}
		
		try {
			Game game = gameManagement.createGame(gameName, player);
			new Thread() {
				private Game game;
				
				public void run() {
					try {
						Thread.sleep(Util.DELAI_START_GAME * 1000);
						
						while (true) {
							game = gameManagement.nextTurn(game);
							Thread.sleep(Util.DELAI_NEXT_TURN * 1000);
						}
						
					} catch (NoPlayersException e) {
					} catch (GameOverException e) {
						System.out.println("Game is over");
					} catch (ArgumentInvalideException e) {
					} catch (InterruptedException e) {
					}
				};
				
				public void param(Game game) {
					this.game = game;
					this.start();
				}
			}.param(game);
			
			request.getSession().setAttribute("game", game);
			RequestDispatcher rd = getServletContext().getNamedDispatcher("LobbyClass");
			rd.forward(request, response);
			return;
		} catch (ArgumentInvalideException e) {
			//On n'est pas sense arriver ici !
		}
		
		request.setAttribute("message", 
				"Une erreur inatendue s'est produite");
		RequestDispatcher rd = getServletContext().getNamedDispatcher(
				"parties");
		rd.forward(request, response);
			
		
	}
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Player player = (Player) Controleur.testConnected(this, request, response);
		if (player == null) return;
		Game game = (Game) Controleur.testGameJoined(this, request, response);
		if (game == null) return;
		
		RequestDispatcher rd = getServletContext().getNamedDispatcher("LobbyClass");
		rd.forward(request, response);
	}
}
